

package 学生管理系统.MOL;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @author czx
 *
 */
public class UserDaoJDBC implements UserDao {



	public TeaUser readTeaUser(String userName) {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		String sql = "select * from teauser where usename like ? ";//表中有id和name这列
		TeaUser use = null;
		try {
			conn = util.JDBCUtil.getConnection();
			pstat = conn.prepareStatement(sql);
			pstat .setString(1, userName);
			rs = pstat.executeQuery();
			while(rs.next()){
				String userName1= rs.getString("usename");
				String  password1 = rs.getString("password");
				 use = new TeaUser (userName1, password1);
			}
		}catch (SQLException sqle) {
			sqle.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			util.JDBCUtil.realeaseAll(rs,pstat, conn);
		}
		return use;
	
	}


	public void diplayAllTeaUser() {
		Connection conn = null;
		Statement stat = null;
		ResultSet rs = null;
		String sql = "select * from teauser";
		try {
			conn = util.JDBCUtil.getConnection();
			stat = conn.createStatement();
			rs = stat.executeQuery(sql);
			while(rs.next()){
				String userName= rs.getString("userName");
				String  password = rs.getString("password");
				System.out.format("该学生的 userName=%s, userName=%s\n", userName,password);
			}
		}catch (SQLException sqle) {
			sqle.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			util.JDBCUtil.realeaseAll(rs,stat, conn);
		}
		
	}


	public boolean writeTeaUser(TeaUser user) {
		Connection conn = null;
		PreparedStatement pstat = null;
		String sql = "insert into teauser(usename,password) values(?,?) ";
		int result = -1;
		
		try {
			conn = util.JDBCUtil.getConnection();
			pstat = conn.prepareStatement(sql);
			pstat.setString(1, user.getUserName());
			pstat.setString(2, user.getPassword());
			result = pstat.executeUpdate();
			
		}catch (SQLException sqle) {
			sqle.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			util.JDBCUtil.realeaseAll(null,pstat, conn);
		}
		return result>0?true:false;
	
	}

    @Override
    public StuUser readStuUser(String userName) {
    Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		String sql = "select * from stuuser where usename like ? ";//表中有id和name这列
		StuUser use = null;
		try {
			conn = util.JDBCUtil.getConnection();
			pstat = conn.prepareStatement(sql);
			pstat .setString(1, userName);
			rs = pstat.executeQuery();
			while(rs.next()){
				String userName1= rs.getString("usename");
				String  password1 = rs.getString("password");
				 use = new StuUser (userName1, password1);
			}
		}catch (SQLException sqle) {
			sqle.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			util.JDBCUtil.realeaseAll(rs,pstat, conn);
		}
		return use;
    }

    @Override
    public void diplayAllStuUser() {
       Connection conn = null;
		Statement stat = null;
		ResultSet rs = null;
		String sql = "select * from stuuser";
		try {
			conn = util.JDBCUtil.getConnection();
			stat = conn.createStatement();
			rs = stat.executeQuery(sql);
			while(rs.next()){
				String userName= rs.getString("userName");
				String  password = rs.getString("password");
				System.out.format("该学生的 userName=%s, userName=%s\n", userName,password);
			}
		}catch (SQLException sqle) {
			sqle.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			util.JDBCUtil.realeaseAll(rs,stat, conn);
		}
    }

    @Override
    public boolean writeStuUser(StuUser user) {
               	Connection conn = null;
		PreparedStatement pstat = null;
		String sql = "insert into stuuser(usename,password) values(?,?) ";
		int result = -1;
		
		try {
			conn = util.JDBCUtil.getConnection();
			pstat = conn.prepareStatement(sql);
			pstat.setString(1, user.getUserName());
			pstat.setString(2, user.getPassword());
			result = pstat.executeUpdate();
			
		}catch (SQLException sqle) {
			sqle.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			util.JDBCUtil.realeaseAll(null,pstat, conn);
		}
		return result>0?true:false;
    }


	
	

}
